<?php

namespace App\Http\Controllers\platform;

use App\Http\Controllers\Controller;
use App\Models\Course;
use App\Models\CourseClass;
use App\Models\CoursePurchase;
use App\Models\CourseXZ;
use App\Models\ProColumn;
use App\Models\StudyUserRecord;
use Illuminate\Http\Request;

class CompanyCourseController extends Controller
{
    public function index(Request $Request)
    {
        $limit = $Request->limit ?: 15;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $is_have_test = $Request->is_have_test ?? '';
        $first_class = $Request->first_class ?? '';
        $second_class = $Request->second_class ?? '';
        $search_key = $Request->search_key ?? '';
        $source = $Request->source ?? '';
        $is_free = $Request->is_free ?? '';

        $sql = Course::select('id','cover_img','name','uniqid','top_category','secondary_category','chapter','doc_num','question_num','is_off','click_num','is_complete','grade','see_type')
        ->where('eid',$Request->eid)->when($is_have_test, function ($query, $is_have_test) {
            if($is_have_test == -1){
                $is_have_test = 0;
            }
            return $query->where('is_have_test', $is_have_test);
        })
        ->when($search_key, function ($query, $search_key) {
            return $query->where('name','like', '%'.$search_key.'%');
        })
        ->when($source, function ($query, $source) {
            $source = $source == 1 ? 1 : 0;
            return $query->where('source_id', $source);
        })
        ->when($is_free, function ($query, $is_free) {
            if($is_free == -1){
                $is_free = 0;
            }
            return $query->where('is_free', $is_free);
        });

        if($Request->is_shop == 1){
            $CoursePurchase = CoursePurchase::where('eid',$Request->eid)->pluck('course_id')->toArray();
            $sql->whereIn('id',$CoursePurchase);
        }
        if($Request->is_shop == -1){
            $CoursePurchase = CoursePurchase::where('eid',$Request->eid)->pluck('course_id')->toArray();
            $sql->whereNotIn('id',$CoursePurchase);
        }

        if($first_class > 0 && !$second_class){
            $second = ProColumn::where('parent_id',$first_class)->pluck('id')->toArray();
            array_push($second,$first_class);
            $class = CourseClass::whereIn('class_id',$second)->pluck('course_id')->toArray();
            $uuid = CourseXZ::whereIn('id',$class)->pluck('uniqid')->toArray();
            $sql->whereIn('uniqid',$uuid);
        }else if($second_class > 0){
            $class = CourseClass::where('class_id',$second_class)->pluck('course_id')->toArray();
            $uuid = CourseXZ::whereIn('id',$class)->pluck('uniqid')->toArray();
            $sql->whereIn('uniqid',$uuid);
        }
        

        if($Request->sort == 1){
            $sql->orderBy('id','asc');
        }

        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->where('is_off',0)->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }
    
}
